package com.tankwar.view.domain;

import com.swak.fx.support.Display;
import com.tankwar.core.Arena;
import com.tankwar.core.ImageDrawAble;
import com.tankwar.view.enums.Direction;
import com.tankwar.view.enums.Tanks;
import com.tankwar.view.resources.Images;

import javafx.scene.image.Image;

/**
 * 坦克
 * 
 * @author lifeng
 * @date 2020年4月19日 下午10:24:39
 */
public class Tank extends ImageDrawAble {

	Direction direction = Direction.UP;
	Tanks type;
	Image upImage;
	Image rightImage;
	Image downImage;
	Image leftImage;

	/**
	 * 攻击冷却时间，毫秒
	 */
	int attackCoolDownTime = 500;

	/**
	 * 移动速度： 刷新速度是 10 ms
	 */
	double speed = 3;

	/**
	 * 是否移动中
	 */
	boolean moving = false;

	/**
	 * 地图
	 */
	Map map = null;

	/**
	 * 舞台： 窗口
	 */
	Arena arena = null;

	/**
	 * 创建坦克
	 * 
	 * @param type 坦克类型
	 */
	public Tank(Tanks type, Arena arena, Map map) {
		super();
		this.arena = arena;
		this.map = map;
		this.type = type;
		this.init();
	}

	/**
	 * 初始化
	 */
	private void init() {
		if (this.type == Tanks.player1) {
			this.upImage = new Image(Display.load(Images.PLAYER1_UP_IMAGE_URL).toExternalForm());
			this.rightImage = new Image(Display.load(Images.PLAYER1_RIGHT_IMAGE_URL).toExternalForm());
			this.downImage = new Image(Display.load(Images.PLAYER1_DOWN_IMAGE_URL).toExternalForm());
			this.leftImage = new Image(Display.load(Images.PLAYER1_LEFT_IMAGE_URL).toExternalForm());
		} else if (this.type == Tanks.player2) {
			this.upImage = new Image(Display.load("/images/tank/player2_up.png").toExternalForm());
			this.rightImage = new Image(Display.load("/images/tank/player2_right.png").toExternalForm());
			this.downImage = new Image(Display.load("/images/tank/player2_down.png").toExternalForm());
			this.leftImage = new Image(Display.load("/images/tank/player2_left.png").toExternalForm());
		} else {
			this.upImage = new Image(Display.load("/images/tank/bot_up.png").toExternalForm());
			this.rightImage = new Image(Display.load("/images/tank/bot_right.png").toExternalForm());
			this.downImage = new Image(Display.load("/images/tank/bot_down.png").toExternalForm());
			this.leftImage = new Image(Display.load("/images/tank/bot_left.png").toExternalForm());
		}

		/**
		 * 设置第一张图片
		 */
		this.setImage(this.upImage);
	}

	// ---------- 转弯 ------------
	protected void turnUp() {
		this.direction = Direction.UP;
		this.setImage(this.upImage);
	}

	protected void turnDown() {
		this.direction = Direction.DOWN;
		this.setImage(this.downImage);
	}

	protected void turnRight() {
		this.direction = Direction.RIGHT;
		this.setImage(this.rightImage);
	}

	protected void turnLeft() {
		this.direction = Direction.LEFT;
		this.setImage(this.leftImage);
	}

	@Override
	public void update() {
		super.update();

		// 移动
		this.move();
	}

	// ---------- 移动 ------------
	protected void move() {

		// 是否可以移动
		if (!this.moving) {
			return;
		}

		// 移动到这一点
		Point rect = this.moveTo();

		// 边界检查
		if (!this.arena.contain(rect.getX(), rect.getY(), this.getWidth(), this.getHeight())) {
			return;
		}

		// 移动
		this.moveTo(rect.getX(), rect.getY());
	}

	protected Point moveTo() {
		double x = this.getX();
		double y = this.getY();
		double offsetX = 0;
		double offsetY = 0;
		switch (direction) {
		case UP:
			y -= speed;
			offsetY = -speed;
			break;
		case DOWN:
			y += speed;
			offsetY = speed;
			break;
		case LEFT:
			x -= speed;
			offsetX = -speed;
			break;
		case RIGHT:
			x += speed;
			offsetX = speed;
			break;
		default:
			break;
		}
		return new Point().setX(x).setY(y).setOffsetX(offsetX).setOffsetY(offsetY);
	}

	// ---------- 攻击 ------------
	protected void attack() {

	}
}
